import fetch from 'dva/fetch';
import { notification } from 'antd';
import { hashHistory } from 'react-router'
import Input from '../../node_modules/._antd@2.13.1@antd/lib/input/Input';

function parseJSON(response) {
  return response.json();
}

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  console.warn(response);
  switch (response.status) {
    case 401:
      notification['error']({
        message: '未授权的请求！',
        description: response.statusText,
      });
      hashHistory.push("/login");
      Promise.reject("UnAuthorization");
      break;
    //  case 500:
    //     notification['error']({
    //       message: '服务异常消息！',
    //       description: response.statusText,
    //     });
    //     break;
    case 504:
      notification['error']({
        message: '请求超时！',
        description: response.statusText,
      });
      break;
    default:
      return response;
      break;
  }
}
function checkAbpStatus(response) {
  if (response && !response.success) {
    const error = response.error;
    notification['error']({
      message: '服务异常。',
      description: error.message + '\r\n' + error.details,
    });
    console.log('=====================');
    console.log(error);
    console.log('=====================');
  }
  return response;
}

/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
export function request(url, options) {
  return fetch(url, options)
    .then(checkStatus)
    .then(parseJSON)
    // .then(data => ({ data }))
    .then(checkAbpStatus)
    .catch(err => ({ err }));
}

export function get(url, options) {
  return fetch(url, {
    ...options,
    mode: 'cors',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + sessionStorage.getItem('accessToken') || ''
    }
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(checkAbpStatus)
    .catch(error => ({ error }));
}

export function post(url, options) {
  return fetch(url, {
    ...options,
    method: 'POST',
    mode: 'cors',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + sessionStorage.getItem('accessToken')
    }
  })
    .then(checkStatus)
    .then(parseJSON)
    // .then(data => ({ data }))
    .then(checkAbpStatus)
    .catch(error => ({ error }));
}
export function put(url, options) {
  return fetch(url, {
    ...options,
    method: 'PUT',
    mode: 'cors',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + sessionStorage.getItem('accessToken')
    }
  })
    .then(checkStatus)
    .then(parseJSON)
    // .then(data => ({ data }))
    .then(checkAbpStatus)
    .catch(error => ({ error }));
}
 
export function del(url, options) {
  return fetch(url, {
    ...options,
    method: 'delete',
    mode: 'cors',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + sessionStorage.getItem('accessToken')
    }
  })
    .then(checkStatus)
    .then(parseJSON)
    // .then(data => ({ data }))
    .then(checkAbpStatus)
    .catch(error => ({ error }));
}

export function getPages(url, current, pageSize, queryStr, queryKey,typeId) {
  let skipCount = 0;
  if (current >= 1) {
    skipCount = (current - 1) * pageSize;
  }
  return request(url + `?InputStr=${queryStr}&SkipCount=${skipCount}&MaxResultCount=${pageSize}&QueryKey=${queryKey}&typeId=${typeId}`);
}
